@extends('admin.common.base-master')

@section('html-head')
    <link href="{{url('css/jquery.form.css')}}" type="text/css" rel="stylesheet" />
    <script src="{{url('/js/lib/jquery.form.js')}}"></script>
    <style>
        .btn_add_classify{
            background: #31bcd3;margin-left:10px;height: 5px;width: 50px;color: white;text-align: center;padding:5px;border-radius:7px;box-shadow: 3px 2px 9px #888888;
        }
    </style>
    @include('UEditor::head')
@endsection

@section('html-body')
    <!-- content start -->
    <div id="content">
        <!-- cur-position start -->
        <div id="cur-position">
            <span class="tit">当前位置：</span>
            <a href="#">内容设置</a>
        </div>
        <!-- cur-position end -->

        <div class="gg-step">
            <form  id="advform">
                <input type="hidden" name="id" id="id" value="@if($sysContents){{$sysContents->id}}@endif"/>

                <div class="gg-list">
                    <label class="tit">文章标题：</label>
                    <div class="cont">
                        <input class="gg-ipt" type="text" placeholder="请输入标题" name="title" id="title" value="@if($sysContents){{$sysContents->title}}@endif" /><cite id="title_re" class="required">*</cite>
                        <p class="text">可输入2-40个字符（一个汉字等于2个字符）</p>
                    </div>
                </div>

                <div class="gg-list">
                    <label class="tit">文章描述：</label>
                    <div class="cont">
                        <textarea name="desc" id="desc" cols="69" rows="5" placeholder="请输入内容">@if($sysContents){{$sysContents->desc}}@endif</textarea><cite id="title_re" class="required">*</cite>
                    </div>
                </div>

                <div class="gg-list">
                    <label class="tit">跳转链接：</label>
                    <div class="cont">
                        <input class="gg-ipt" type="text" placeholder="请输入跳转链接" name="link_url" id="link_url" value="@if($sysContents){{$sysContents->link_url}}@endif" /><cite id="title_re" class="required">*</cite>
                        <p class="text">可输入2-40个字符（一个汉字等于2个字符）</p>
                    </div>
                </div>

                {{--<div class="gg-list">--}}
                    {{--<label class="tit">所属类别</label>--}}
                    {{--<div class="cont">--}}
                        {{--<select name="classify" id="classify">--}}
                            {{--@if(count($classifys) > 0)--}}
                                {{--@foreach($classifys as $one)--}}
                                    {{--<option value="{{$one->id}}" @if($sysContents && $sysContents->classify_id == $one->id)selected @endif>{{$one->name}}</option>--}}
                                {{--@endforeach--}}
                            {{--@endif--}}
                        {{--</select>--}}
                    {{--</div>--}}
                {{--</div>--}}

                <div class="gg-list" id="tupian_img">
                    <label class="tit">展示图</label>
                    <div class="demo" style="display: inline-block; padding: 0; margin: 0; *display: inline; *zoom:1;" >
                        <input type = "hidden" name = "pic_img" id = "pic_img2" value = "" height="70px" >
                        <div id="showimg">
                        </div>
                        <div class="btn" style="background: #00A2D4;">
                            <span id="inputVideo">添加图片</span>
                            <input id="img_id" type="hidden" name="thumb_id" value="">
                        </div>
                        <div class="progress" >
                            <span class="bar"></span><span class="percent">0%</span >
                        </div>
                        <p class="text" id="uploadthumb">上传图片（图片格式为jpg、png、gif）</p>

                    </div>
                </div>

                <div class="gg-list" data-id="ggwz">
                    <label class="tit">文章内容：</label>
                    <div class="cont">
                        <textarea placeholder="请输入文章内容" name="description" id="aa"></textarea>
                    </div>
                </div>

                <input type="hidden" id="answer_id" value="" />
            </form>
            <div class="gg-list" >
                <label class="tit">&nbsp;</label>
                <div class="cont" >
                    <a href="javascript:;" class="next-step" id="btn_send">提交</a>
                </div>
            </div>
        </div>
    </div>

    <!-- content end -->
    <script>
        $(function() {

            <!-- 实例化编辑器 -->
            var ue = UE.getEditor('aa',{
                autoHeightEnabled: true,
                initialFrameHeight:400,
                initialFrameWidth:700,
                toolbars: [
                    ['fullscreen', 'source','undo','redo','|','fontsize','horizontal','|','simpleupload','insertvideo','removeformat'],
                    ['bold', 'italic', 'underline', 'forecolor', 'backcolor','|','indent','|','justifyleft', 'justifyright', 'justifycenter', 'justifyjustify', '|', 'rowspacingtop', 'rowspacingbottom', 'lineheight','|','insertunorderedlist', 'insertorderedlist','|', 'imagenone','imageleft','imageright','imagecenter','|','link']
                ]
            });

            @if($sysContents)
            ue.ready(function() {
                ue.setContent('<?php echo $sysContents->content ?>');
            });
            @endif

            $('#btn_send').click(function () {
                var title    = $('#title').val();
                var content  = '';
                var id = $('#id').val();
                var desc = $("#desc").val();
                var linkUrl = $('#link_url').val();
                var imgID   = $("#img_id").val();


                ue.ready(function() {
                    content = ue.getContent();
                });

                $.ajax({
                    url:"{{route('Api.Admin.sys.setContents')}}",
                    type:'post',
                    dataType:'json',
                    data:{
                        title:title,
                        content:content,
                        id:id,
                        desc:desc,
                        link_url:linkUrl,
                        img_id:imgID
                    },
                    success:function (obj) {
                        if(obj.level == 'success'){
                            noty({text:obj.message,type:obj.level,timeout:600,
                                callback:{
                                    afterClose:function(){
                                        window.location.href="{{route('Admin.Home.contentsList')}}";
                                    }
                                }
                            });
                        }else {
                            noty({text:obj.message,type:obj.level,timeout:600});
                        }
                    },
                    error:function (obj) {
                        console.log(obj);
                    }
                });
            });

            //图片上传代码
            var uploader = new plupload.Uploader({
                runtimes: 'html5,flash,silverlight,html4',
                browse_button: 'inputVideo',
                url: '{{route("Api.Upload.Image")}}',

                chunk_size: '20mb',
                filters: {
                    max_file_size: '20mb',
                    mime_types: [
                        {title: "Image files", extensions: "jpg,gif,png"}
                    ],
                    prevent_duplicates: false
                },
                multi_selection: true,
                unique_names: true,

                flash_swf_url: '{{ url('vendor/plupload-2.1.2/Moxie.swf') }}',
                silverlight_xap_url: '{{ url('vendor/plupload-2.1.2/Moxie.xap') }}',

                file_data_name: 'photo',
                headers: {
                    Accept: 'application/json'
                },

                multipart_params: {},

                init: {
                    PostInit: function () {
                    },

                    FilesAdded: function (up, files) {
                        up.start();
                    },

                    BeforeUpload: function (up, file) {
                        up.settings.multipart_params.file_name = file.name;
                    },

                    UploadProgress: function (up, file) {
                        $('#uploadthumb').html('<span style="color:red">上传中...</span>');
                    },

                    Error: function (up, err) {
                        var responseObj = '', responseMessage = '';
                        try {
                            responseObj = $.parseJSON(err.response);
                            responseMessage = responseObj.message;
                        } catch ($e) {

                        }

                        noty({
                            type: 'error',
                            text: err.message + ' ' + responseMessage,
                            timeout: 800
                        });
                    },

                    FileUploaded: function (up, file, info) {
                        var responseObj = '', responseMessage = '';
                        try {
                            responseObj = $.parseJSON(info.response);
                            responseMessage = responseObj.message;
                        } catch ($e) {

                        }
                        if (responseObj.level == "success") {
                            $('#img_id').val(responseObj.data.id);
                            $('#uploadthumb').html('<span style="color:green">上传成功</span>');
                        } else {
                            noty({type: responseObj.level, text: responseObj.message, timeout: 800});
                            $('#uploadthumb').html('<span style="color: red">上传失败，请重新上传！</span>');
                        }

                    }
                }
            });
            uploader.init();
        });
    </script>
@endsection